﻿Imports System.Xml
Imports System.Data.OleDb

Public Class Rss
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.ContentType = "application/rss+xml"
        Response.ContentEncoding = Encoding.UTF8
        Dim rsswriter_JiangJiawei As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
        'opening
        WriteRssOpening(rsswriter_JiangJiawei)
        'body
        Dim chineseFoodRssTable As DataTable = GetDateSet()
        WriteRssBody(rsswriter_JiangJiawei, chineseFoodRssTable)
        'ending
        WriteRssEnding(rsswriter_JiangJiawei)
        rsswriter_JiangJiawei.Flush()
        Response.End()
    End Sub

    Private Sub WriteRssOpening(ByVal rsswriter_JiangJiawei As XmlTextWriter)
        rsswriter_JiangJiawei.WriteStartElement("rss")
        rsswriter_JiangJiawei.WriteAttributeString("version", "2.0")
        rsswriter_JiangJiawei.WriteStartElement("channel")
        rsswriter_JiangJiawei.WriteElementString("title", "Chinese Food")
        rsswriter_JiangJiawei.WriteElementString("link", "ChineseFoodList.aspx")
        rsswriter_JiangJiawei.WriteElementString("description", "A Bite of China")
    End Sub

    Private Sub WriteRssBody(ByVal rsswriter_JiangJiawei As XmlTextWriter, ByVal data As DataTable)
        For Each chineseFoodColumnInDb As DataRow In data.Rows
            rsswriter_JiangJiawei.WriteStartElement("item")
            rsswriter_JiangJiawei.WriteElementString("title", chineseFoodColumnInDb(1).ToString)
            rsswriter_JiangJiawei.WriteElementString("link", "ChineseFoodDetail.aspx?ID=" & chineseFoodColumnInDb(0).ToString)
            'rsswriter_JiangJiawei.WriteElementString("author", "Jiang Jiawei") 'I want to show the name who submited the food, I'll fix it later
            'rsswriter_JiangJiawei.WriteElementString("pubDate", "2013") 'The date is not correct specification, I'll check the specification, then fix it in final assignment
            rsswriter_JiangJiawei.WriteElementString("description", "Area of Origin: " & chineseFoodColumnInDb(3).ToString) '& "/ " & "Popularity (1 is low and 5 is high): " & chineseFoodColumnInDb(5).ToString)
            'The above line, in fact, I dont want to use "/", but vbnewline doesn't work, i'll google more articles to fix it
            rsswriter_JiangJiawei.WriteEndElement()
        Next
    End Sub

    Private Sub WriteRssEnding(ByVal rsswriter_JiangJiawei As XmlTextWriter)
        rsswriter_JiangJiawei.WriteEndElement()
        rsswriter_JiangJiawei.WriteEndElement()
    End Sub

    Private Function GetDateSet() As DataTable
        Dim chineseFoodRssTable = New DataTable()
        Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings("ChineseFoodConn").ConnectionString)
        Dim sql = New String("SELECT * FROM [ClassicChineseFood]")
        Dim jiang = New OleDbDataAdapter(sql, conn)
        jiang.Fill(chineseFoodRssTable)
        Return chineseFoodRssTable
    End Function

End Class